package application;

import java.util.ArrayList;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

public class Plot {
	
	public String Name;
	public String Description;
	public ArrayList<String> scenes;
	public String faction;
	public Plot(String name, String description,
			ArrayList<String> scenes, String faction) {
		super();
		this.Name = name;
		this.Description = description;
		this.scenes = scenes;
		this.faction = faction;
	}
	public Plot() {
		super();
		this.Name = "new plot";
		this.Description = "this is a plot";
		this.scenes = new ArrayList<String>();
		this.faction = "no faction selected";
	}
	public void dbCreatePlot (String dbFile){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{

	        		db.store(this);


		}
		finally{
			db.close();
		}
	}
	
	public void dbEditPlot (String dbFile, Plot c){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Plot> result = db
		        .queryByExample(Plot.this);
				Plot found = (Plot) result.next();
				db.delete(found);
				this.setDescription(c.getDescription());
				this.setFaction(c.getFaction());
				this.setName(c.getName());
				this.setScenes(c.getScenes());
				
				db.store(this);
				
			}
			catch(IllegalStateException e){
				
			}

		}
		finally{
			db.close();
		}
	}
	
	public void dbDeletePlot (String dbFile){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Plot> result = db
		        .queryByExample(Plot.this);
				Plot found = (Plot) result.next();
				db.delete(found);
			}
			catch(IllegalStateException e){
				
			}

		}
		finally{
			db.close();
		}
	}
	
	public void dbSceneDelete(String dbFile, String s){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{

			ObjectSet<Plot> result=db.queryByExample(Plot.class);
	        while(result.hasNext()) {
	        	
	        	Plot tmp = (Plot) result.next();
	        	if(this.toString().equals(tmp.toString())){
	        		this.scenes.remove(s);
	        		db.store(this);
	        		db.delete(tmp);
	        	}
	        }
		}
		finally{
			db.close();
		}
	}
	
	public void dbSceneAdd(String dbFile, String s){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{

			ObjectSet<Plot> result=db.queryByExample(Plot.class);
	        while(result.hasNext()) {
	        	
	        	Plot tmp = (Plot) result.next();
	        	if(this.toString().equals(tmp.toString())){
	        		this.scenes.add(s);
	        		db.store(this);
	        		db.delete(tmp);
	        	}
	        }
		}
		finally{
			db.close();
		}
	}
	
	public String toString() {
		return Name;
	}
	public String getName() {
		return Name;
	}
	public String getDescription() {
		return Description;
	}

	public void setName(String name) {
		Name = name;
	}
	public void setDescription(String description) {
		Description = description;
	}

	public String getFaction() {
		return faction;
	}
	public void setFaction(String faction) {
		this.faction = faction;
	}
	public ArrayList<String> getScenes() {
		return scenes;
	}

	public void setScenes(ArrayList<String> scenes) {
		this.scenes = scenes;
	}

	
	
	
	

}
